// 
//  FDHighScoresView.m
//  FiveDice
//  
//  Created by Chris Born.
//  Copyright 2007 Borngraphics. All rights reserved.
//  
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//  
//  By contributing code you grant Chris Born and Borngraphics an
//  unlimited, non-exclusive license to your contribution.
//  
//  For support, questions, commercial use, etc...
//  E-Mail: chris <at> borngraphics <dot> com
// 

#import "FDHighScoresView.h"

@implementation FDHighScoresView

-(id)initWithFrame:(struct CGRect)frame {
	if ((self == [super initWithFrame: frame]) != nil) 
	{
		NSLog(@"GameMenuView %s", _cmd);
		_contentRect = [UIHardware fullScreenApplicationContentRect];
		_offScreenRect = frame;
		_onScreenRect = frame;
		_onScreenRect.origin.x = 0.0f;
	
		defaults = [[FDDefaultsController alloc] init];

		UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(7,0,309,398)];
		[background setImage:[UIImage applicationImageNamed:@"banner.png"]];	
		[self addSubview:background];
		[background release];
		
		// The title High Scores
		UITextLabel	*cardLabel = [[UITextLabel alloc] initWithFrame:CGRectMake(29, 28, 263, 35)];
		[cardLabel setText:@"High Scores"];
		[cardLabel setCentersHorizontally:YES];
		[cardLabel setShadowOffset:CGSizeMake(0,1)];
		
		struct __GSFont *complFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Georgia" traits:2 size:20];
		[cardLabel setFont:complFont];
		
		float clearColor[4] = {1, 1, 1, 0};
		[cardLabel setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), clearColor)];
		[self addSubview:cardLabel];
		[cardLabel release];
		
		UIView	*line = [[UIView alloc] initWithFrame:CGRectMake(29,64,263,1)];

		float blackColor[4] = {0, 0, 0, 1};
		[line setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), blackColor)];
		[self addSubview:line];
		[line release];
				
		[self setEnabled:YES];
		[self setTapDelegate:self];
		
		[self createHighScoresTable];
	}
	return self;
}

#pragma mark Show and Hide Methods
- (void)show
{
	translate = [[UITransformAnimation alloc] initWithTarget:self];
	animator = [[UIAnimator alloc] init];

	struct CGAffineTransform trans = CGAffineTransformMakeTranslation(_contentRect.size.width, 0);
	
	[self setFrame:_offScreenRect];
	[translate setStartTransform:CGAffineTransformMake(1,0,0,1,0,0)];
	[translate setEndTransform:trans];
	[animator addAnimation:translate withDuration:.5 start:YES];

	[highScoresTable scrollRowToVisible:0];
}

- (void)hide
{
	translate = [[UITransformAnimation alloc] initWithTarget:self];
	animator = [[UIAnimator alloc] init];
	
	struct CGAffineTransform trans = CGAffineTransformMakeTranslation(_contentRect.size.width, 0);
	
	[self setFrame:_onScreenRect];
	[translate setStartTransform:trans];
	[translate setEndTransform:CGAffineTransformMake(1,0,0,1,0,0)];
	[animator addAnimation:translate withDuration:.5 start:YES];
	[delegate hidingHighScores];
}

#pragma mark HighScore Table
- (void)createHighScoresTable {
		UITableColumn *position = [[UITableColumn alloc]
			initWithTitle:@"Position"
			identifier:@"position"
			width:40];		
	
		UITableColumn *names = [[UITableColumn alloc]
			initWithTitle:@"Names"
			identifier:@"names"
			width:170];
			
		UITableColumn *scores = [[UITableColumn alloc]
			initWithTitle:@"Scores"
			identifier:@"scores"
			width:50];
				 
		NSLog(@"HighScores >> init table...");
		highScoresTable = [[UITable alloc] initWithFrame:CGRectMake(29, 65, 263, 315)];

		[highScoresTable addTableColumn: position];
		[highScoresTable addTableColumn: names];
		[highScoresTable addTableColumn: scores];
		
		/*
			Don't forget to release these things. A lot of early iPhone apps were not releasing table
			related object after adding them. This may not seem like a big deal, but it is a good habit
		*/
		[position release];
		[names release];
		[scores release];
		
		[highScoresTable setSeparatorStyle:1];
		[highScoresTable setDelegate: self];
		[highScoresTable setDataSource: self];
		[highScoresTable setRowHeight:38.0];

		float _clearColor[4] = {1, 1, 1, 0};
		[highScoresTable setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), _clearColor)];
		
		[self reloadData];				
		[self addSubview: highScoresTable];
		[highScoresTable release];
}

- (UITableCell *)table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col
{
	NSLog(@"%s", _cmd);

	NSMutableArray		*array = [[NSMutableArray alloc] init];
	[array setArray:[defaults highScoresSorted]];	
	NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[array objectAtIndex:row]];
	
	UIImageAndTextTableCell *cell = [[UIImageAndTextTableCell alloc] init];
	[cell setShowSelection:NO];
	
	if ([[col identifier] isEqualToString:@"names"]) {
		NSLog(@"%s HS Cell Name: %@", _cmd,[dict objectForKey:@"Name"]);
		[cell setTitle:[dict objectForKey:@"Name"]];
	} else if ([[col identifier] isEqualToString:@"scores"]) {
		[cell setTitle:[[dict objectForKey:@"Score"] stringValue]];
	}else if ([[col identifier] isEqualToString:@"position"]) {
		[cell setTitle:[NSString stringWithFormat:@"%i",row+1]];
	}
	
	[array release];
	
	/*
		We can't keep allocing these things and not releasing them. Another problem with early
		iPhone apps. Lots of code was allocing these cells in this delegate method without 
		releasing them. OUCH! You see, everytime you reloadData the table comes back in here to
		get its cells. Thus allocing more, and more, and more. We autorelease because we are about
		to return the cell to the table, where it will be retained. When the table goes to reload
		cell data, it will release the old cell and come here to get a new one.
	*/
	[cell autorelease];
	return cell;
}

- (void)reloadData
{
	[highScoresTable reloadData];
}

- (void)tableRowSelected:(NSNotification *)notification 
{
	NSLog(@"%s", _cmd);
}

- (int)numberOfRowsInTable: (UITable *)table
{
	return [[NSArray arrayWithArray:[defaults highScoresSorted]] count];
}

#pragma mark Tap Delegate
- (void)mouseUp:(struct __GSEvent *)event {
	[self hide];
}

#pragma mark Delegate
- (id)delegate
{
	return delegate;
}

- (void)setDelegate:(id)newDelegate
{
	delegate = newDelegate;
}

#pragma mark Dealloc
- (void)dealloc
{
	[animator release];
	[translate release];
	[super dealloc];
}
@end